import request from "@/utils/request";
import storage from "good-storage";
import cookies from "vue-cookies";

export function isIdCard(num) {
    if (!num) {
        return false
    }
    num = num.toUpperCase() // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
    if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
        // alert('输入的身份证号长度不对，或者号码不符合规定！n15位号码应全为数字，18位号码末位可以为数字或X。');
        return false
    }
    // 校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
    // 下面分别分析出生日期和校验位
    let re
    const len = num.length
    if (len === 15) {
        re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
        const arrSplit = num.match(re) // 检查生日日期是否正确
        const dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
        const bGoodDay = (dtmBirth.getYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
        if (!bGoodDay) {
            // alert('输入的身份证号里出生日期不对！');
            return false
        } else {
            // 将15位身份证转成18位//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
            const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
            let nTemp = 0
            num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6)
            for (let i = 0; i < 17; i++) {
                nTemp += num.substr(i, 1) * arrInt[i]
            }
            num += arrCh[nTemp % 11]
            return true
        }
    }
    if (len === 18) {
        re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
        const arrSplit = num.match(re) // 检查生日日期是否正确
        const dtmBirth = new Date(arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
        const bGoodDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
        if (!bGoodDay) {
            // alert('输入的身份证号里出生日期不对！');
            return false
        } else {
            // 检验18位身份证的校验码是否正确。
            // 校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
            const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
            let nTemp = 0
            for (let i = 0; i < 17; i++) {
                nTemp += num.substr(i, 1) * arrInt[i]
            }
            const valnum = arrCh[nTemp % 11]
            if (valnum !== num.substr(17, 1)) {
                // alert('18位身份证的校验码不正确！应该为：'+valnum);
                return false
            }
            return true
        }
    }
    return false
}

export async function getLoginUser() {
    //  获取用户token,判断是否与缓存中的token一致
    let userToken = cookies.get("user_token");
    let localToken = storage.get("user_token");
    //  如果token不存在,则判断用户未登录
    if (userToken === null) return null;
    //  如果cookie中的token与缓存中的token不一致,则删除当前登录用户
    if (userToken !== localToken) storage.remove("loginUser");
    //  获取当前登录用户,如果没有,则重新获取用户信息
    let loginUser = storage.get("loginUser");
    if (loginUser) return loginUser;

    loginUser = await request.get("/realmock/getUserInfo");

    storage.set("loginUser", loginUser);
    return loginUser;
}

export function countDownFormat(second) {
    if (second <= 0) return "00:00:00";
    let d = parseInt(second / (24 * 60 * 60))
    d = d < 10 ? "0" + d : d
    let h = parseInt(second / (60 * 60));
    h = h < 10 ? "0" + h : h
    let m = parseInt(second / 60 % 60);
    m = m < 10 ? "0" + m : m
    let s = parseInt(second % 60);
    s = s < 10 ? "0" + s : s
    return h + ':' + m + ':' + s
}

/**
 * 将文本中的html格式化
 * @param content
 * @returns {*}
 */
export function formatHTML(content) {
    // content = (content).replace(/\</g, '&lt') + "";
    let image_preg = /\[img=([^\(\)\]]+)\((\d+),(\d+)\)\]/, max_width = 300;
    let match_data = content.match(image_preg);
    while (match_data !== null) {
        //  创建图片
        let img = document.createElement("img");
        img.style.maxWidth = max_width;
        img.setAttribute("src", match_data[1]);
        //  如果图片的宽度小于设定的最小宽度,则设置图片的宽度值
        if (match_data[2] <= max_width) img.style.width = match_data[2];
        content = content.replace(match_data[0], img.outerHTML);
        match_data = content.match(image_preg);
    }

    //  替换换行符
    content = content.replace(/\n/g,'<br/>')
    return content;
}

export function getRequestQuery(url) {
    if (!url) url = window.location.href;
    let query = url.split("?").pop().split("&");
    let result = {};
    query.forEach((item) => {
        let data = item.split("=");
        result[data[0]] = data[1];
    });
    return (result)
}
